package com.wokeMy.view.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wokeMy.view.model.YkHeData;
import com.zhongcai.online.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/2/8 0008.
 */

public class YingkAdapter extends RecyclerView.Adapter<YingkAdapter.MyViewHolder> {
    private Context context;
    private ArrayList<YkHeData> data;
    private ArrayList<Integer> check;
    private boolean isSigle;  // true 只能选一个

    private MyItemClickListener mItemClickListener;
    public YingkAdapter(Context context, ArrayList<YkHeData> data,boolean isSigle) {
        check= new ArrayList<>();
        this.context = context;
        this.data = data;
        this.isSigle = isSigle;
        initState();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //将全局的监听传递给holder
        MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_yingk, null),mItemClickListener);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tvNum.setText(data.get(position).getNumber()+"");
        holder.tvMoney.setText(data.get(position).getMoney()+"");
        if (data.get(position).getMoney().equals("")){
            holder.tvMoney.setVisibility(View.GONE);
        }else {
            holder.tvMoney.setVisibility(View.VISIBLE);
        }
        if (check.get(position)==0){
            holder.tvNum.setTextColor(context.getResources().getColor(R.color.num_yk));
            holder.tvMoney.setTextColor(context.getResources().getColor(R.color.money_yk));
            holder.llbiankuang.setBackgroundResource(R.drawable.yk_biankuang);
        }else {
            holder.tvNum.setTextColor(context.getResources().getColor(R.color.green_yk));
            holder.tvMoney.setTextColor(context.getResources().getColor(R.color.green_yk));
            holder.llbiankuang.setBackgroundResource(R.drawable.yk_biankuang_check);
        }

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    //这里定义的是子项的类，不要在这里直接对获取对象进行操作
    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView tvNum;
        TextView tvMoney;
        LinearLayout llbiankuang;

        private MyItemClickListener mListener;

        public MyViewHolder(View itemView, MyItemClickListener myItemClickListener) {
            super(itemView);
            tvNum=(TextView)itemView.findViewById(R.id.item_num_yingk);
            tvMoney=(TextView)itemView.findViewById(R.id.item_money_yingk);
            llbiankuang=(LinearLayout)itemView.findViewById(R.id.item_bk_yingk);
            //将全局的监听赋值给接口
            this.mListener = myItemClickListener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (check.size()!=0){
                Log.e("YingkAdapter",""+getLayoutPosition()+" "+isSigle);
                if (isSigle){
                    Log.e("check.get(getLayoutP",""+check.get(getLayoutPosition())+" "+getLayoutPosition());
                    if (check.get(getLayoutPosition())==0){
                        for (int i = 0; i < check.size(); i++) {
                            if (getLayoutPosition()==i){
                                check.set(i,1);
                            }else {
                                check.set(i,0);
                            }
                        }
                    }
                }else {
                    if (check.get(getLayoutPosition())==0){
                        tvNum.setTextColor(context.getResources().getColor(R.color.green_yk));
                        tvMoney.setTextColor(context.getResources().getColor(R.color.green_yk));
                        llbiankuang.setBackgroundResource(R.drawable.yk_biankuang_check);
                        check.set(getLayoutPosition(),1);
                    }else {
                        tvNum.setTextColor(context.getResources().getColor(R.color.num_yk));
                        tvMoney.setTextColor(context.getResources().getColor(R.color.money_yk));
                        llbiankuang.setBackgroundResource(R.drawable.yk_biankuang);
                        check.set(getLayoutPosition(),0);
                    }
                }
                Log.e("YingkAdapter",""+check.toString());
                notifyDataSetChanged();
            if (mListener != null) {
                mListener.onItemClick(v, getPosition());
            }

            }
        }
    }
   public void initState(){
       for (int i = 0; i < data.size(); i++) {
           check.add(0);
       }
    }
    public ArrayList<Integer> getCheck(){
       return check;
    }
    public void setCheck(ArrayList<Integer> checks){
//        this.check.clear();
        this.check=checks;
        Log.e("setCheck",""+this.check.toString());
    }
    public void setSigle(boolean isSigle){
        this.isSigle=isSigle;
    }
    public boolean getSigle(){
        return isSigle;
    }
    /**
     * 创建一个回调接口
     */
    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }
    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     *
     * @param myItemClickListener
     */
    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
